//
//  OrderDetailInfoCell.m
//  danpin
//
//  Created by chuangjia on 29/10/2015.
//  Copyright © 2015 chuangjia. All rights reserved.
//

#import "OrderDetailInfoCell.h"

#import "TTTAttributedLabel.h"
#import "OrderInfo.h"
#import "CalcLabel.h"
@interface OrderDetailInfoCell ()
{
    
}
@property(nonatomic,strong)UILabel *sectionLabel;
@property(nonatomic,strong)UIButton *helpbtn;
@property(nonatomic,strong)UIView *lineview;
//
@property(nonatomic, strong) TTTAttributedLabel* title;
@property(nonatomic,strong) UIImageView *titleIcon;
@property(nonatomic,strong) UILabel * lbQty;
@property(nonatomic,strong) UILabel * lbSizeColor;
@property(nonatomic,strong)UIView *line2view;
//


@end
@implementation OrderDetailInfoCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor=[UIColor clearColor];
        _sectionLabel =[[UILabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH/2,20)];
        _sectionLabel.backgroundColor = [UIColor clearColor];
        _sectionLabel.textColor = CONTEXT_COLOR;
        _sectionLabel.textAlignment = NSTextAlignmentLeft;
        _sectionLabel.font = FONT(12);
        [self.contentView addSubview:_sectionLabel];
     
        _lineview =[[UIView alloc] initWithFrame:CGRectZero];
        _lineview.backgroundColor=UIColorFromRGB(0xe5e5e5);
        [self.contentView addSubview:_lineview];
        
        //
        _title =[[TTTAttributedLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH*8,PHOTO_FRAME_WIDTH, DEVICE_WIDTH-PHOTO_FRAME_WIDTH* 10,40)];
        _title.backgroundColor = [UIColor clearColor];
        _title.textColor = [UIColor darkTextColor];//UIColorFromRGB(0x808080);
        _title.textAlignment = NSTextAlignmentLeft;
        _title.lineBreakMode = NSLineBreakByWordWrapping|NSLineBreakByTruncatingTail;
        _title.numberOfLines=0;
        _title.font = FONT(13);
        [self.contentView addSubview:_title];
        
        _titleIcon =[[UIImageView alloc] initWithFrame:CGRectZero];
        _titleIcon.frame =CGRectMake(PHOTO_FRAME_WIDTH,PHOTO_FRAME_WIDTH, PHOTO_FRAME_WIDTH*6, PHOTO_FRAME_WIDTH*6);
        _titleIcon.contentMode = UIViewContentModeScaleAspectFit;
        [self.contentView addSubview:_titleIcon];
        
        
        _lbSizeColor =[[UILabel alloc] initWithFrame:CGRectZero];
        _lbSizeColor.backgroundColor = [UIColor clearColor];
        _lbSizeColor.textColor =  [UIColor darkGrayColor];
        _lbSizeColor.lineBreakMode = NSLineBreakByTruncatingTail;
        _lbSizeColor.textAlignment = NSTextAlignmentLeft;
        _lbSizeColor.font =  FONT(11);
        _lbSizeColor.numberOfLines=0;
        _lbSizeColor.frame =CGRectMake(PHOTO_FRAME_WIDTH*8, PHOTO_FRAME_WIDTH*5.5, DEVICE_WIDTH - PHOTO_FRAME_WIDTH*10, PHOTO_FRAME_WIDTH*2);
        [self.contentView addSubview:_lbSizeColor];
        
        _lbQty =[[UILabel alloc] initWithFrame:CGRectZero];
        _lbQty.backgroundColor = [UIColor clearColor];
        _lbQty.textColor =  [UIColor darkGrayColor];
        _lbQty.font =  FONT(12);
        _lbQty.numberOfLines=0;
        _lbQty.textAlignment = NSTextAlignmentRight;
        _lbQty.frame =CGRectMake(DEVICE_WIDTH - 120, PHOTO_FRAME_WIDTH*5.5, 110, PHOTO_FRAME_WIDTH*2);
        
        [self.contentView addSubview:_lbQty];
        
        _line2view =[[UIView alloc] initWithFrame:CGRectZero];
        _line2view.backgroundColor=UIColorFromRGB(0xe5e5e5);
        [self.contentView addSubview:_line2view];
   
        
    }
    return self;
}
-(void)dealloc
{
    self.title =nil;
    self.actionBlock =nil;
    self.data=nil;
    self.indexPath=nil;
    self.other=nil;
    self.key=nil;
    
    //    DLog(@"%s", __PRETTY_FUNCTION__);
}
- (void)drawRect:(CGRect)rect {
    
    [super drawRect:rect];
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
}
/// 根据数据模型来显示内容
- (void)showInfo:(id)model key:(id)key indexPath:(NSIndexPath *)indexPath
{
    self.key =key;
    self.indexPath=indexPath;
    NSDictionary *dict =(NSDictionary*)model;
    
    self.data =model;
    
    //
    NSDictionary *goods_info =[dict dictionaryObjectForKey:@"goods_info"];
    if (goods_info==nil || ![goods_info isKindOfClass:[NSDictionary class]]) {
        return;
    }
    _sectionLabel.text =[goods_info stringForKey:@"goods_from" withDefaultValue:@""];
    _sectionLabel.frame =CGRectMake(PHOTO_FRAME_WIDTH, 0,DEVICE_WIDTH, 40);
    
    _helpbtn.frame =CGRectMake(DEVICE_WIDTH-26,(40-16)/2, 16, 16);
    _lineview.frame =CGRectMake(PHOTO_FRAME_WIDTH, 39.5,DEVICE_WIDTH, 0.5);
    //
   
    
    NSString *urlstr =[goods_info stringForKey:@"good_img" withDefaultValue:@""];
    if (urlstr.length>0) {
        _titleIcon.frame =CGRectMake(PHOTO_FRAME_WIDTH,PHOTO_FRAME_WIDTH*5, PHOTO_FRAME_WIDTH*6, PHOTO_FRAME_WIDTH*6);
        [_titleIcon sd_setImageWithURL:[NSURL URLWithString:urlstr]];
    }
    float fheight = 0;
    NSString *title =[goods_info stringForKey:@"good_name" withDefaultValue:@""];
    if (title.length>0) {
        [self.title setText:title afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString  *(NSMutableAttributedString *attributedString)
         {
             [attributedString addAttribute:NSFontAttributeName
                                      value:FONT(12)
                                      range:NSMakeRange(0,[attributedString length])];

              //设置字体颜色
             [attributedString addAttribute:(id)kCTForegroundColorAttributeName value:TITLE_COLOR range:NSMakeRange(0,[attributedString length])];
             
             CGFloat lineSpace = 1.0f;
             CTParagraphStyleSetting lineSpaceStyle;
             lineSpaceStyle.spec = kCTParagraphStyleSpecifierLineSpacingAdjustment;
             lineSpaceStyle.valueSize = sizeof(lineSpace);
             lineSpaceStyle.value =&lineSpace;
             
             //设置文本段间距
             CGFloat paragraphSpacing = 0.0f;
             CTParagraphStyleSetting paragraphSpaceStyle;
             paragraphSpaceStyle.spec = kCTParagraphStyleSpecifierParagraphSpacing;
             paragraphSpaceStyle.valueSize = sizeof(CGFloat);
             paragraphSpaceStyle.value = &paragraphSpacing;
             
             //创建设置数组
             CTParagraphStyleSetting settings[ ] ={lineSpaceStyle,paragraphSpaceStyle};
             CTParagraphStyleRef style = CTParagraphStyleCreate(settings ,2);
             
             //给文本添加设置
             [attributedString addAttribute:(id)kCTParagraphStyleAttributeName value:(id)CFBridgingRelease(style) range:NSMakeRange(0 , [attributedString length])];
             
             
    //         CFRelease(helveticaBold);
             
             return attributedString;
             
         }];
        
        fheight = [CalcLabel getAttributedStringHeightWidthValue:(DEVICE_WIDTH- PHOTO_FRAME_WIDTH*9) str:self.title.attributedText];
        
        if (fheight>40) {
            fheight=40;
        }
        
        self.title.frame = CGRectMake(PHOTO_FRAME_WIDTH*8, PHOTO_FRAME_WIDTH*5,DEVICE_WIDTH- PHOTO_FRAME_WIDTH*9, fheight);
    }
    self.lbSizeColor.frame = CGRectMake(PHOTO_FRAME_WIDTH*8, PHOTO_FRAME_WIDTH*5.5 + fheight + PHOTO_TIME_PADDING,DEVICE_WIDTH- PHOTO_FRAME_WIDTH*14, 20);
    
    NSString *good_intro =[goods_info stringForKey:@"good_intro" withDefaultValue:@""];
    self.lbSizeColor.text =good_intro;
    
    self.lbQty.frame =CGRectMake(DEVICE_WIDTH - 120, PHOTO_FRAME_WIDTH*5.5 + fheight + PHOTO_TIME_PADDING, 110, PHOTO_FRAME_WIDTH*2);
    self.lbQty.text = [NSString stringWithFormat:@"¥ %.0f x %d", [[goods_info numberStringForKey:@"good_fee"] floatValue], [[dict numberStringForKey:@"num"] intValue]];
    
    _line2view.frame =CGRectMake(0, 119.5,DEVICE_WIDTH, 0.5);
    
    [self layoutSubviews];
}
-(void)setCallBack:(void(^)(BEventType,UIView*,id,id,NSIndexPath *))callback
{
    self.actionBlock=callback;
}
-(void)clickHelpEvent:(id)sender
{
    UIButton *btn = (UIButton *)sender;
    NSString *str =[NSString stringWithFormat:@"%ld",(long)btn.tag];
    if (self.actionBlock) {
        self.actionBlock(BEventType_help,nil,self.data,str,self.indexPath);
    }
}
/// 返回Cell高度
+ (CGFloat)returnCellHeight:(id)model
{
    return PHOTO_FRAME_WIDTH*12;
}


@end
